<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\CollectHouse;
use App\Models\House;
use App\Models\MyAnnounce;
use App\Models\Problem;
use App\Services\Common;
use Illuminate\Http\Request;
use Predis\Client;

class Mine extends Controller
{
    //问题反馈
    public function problem(Request $request)
    {
        $data = $request->except('token');
        if(!preg_match("/^1[3456789]\d{9}$/", $data['phone'])){
            return ['code'=>4003,'msg'=>'手机号不正确'];
        }
        $token = Common::getAccessToken(config('admin.mgc.appid'), config('admin.mgc.secretkey'));

        $url = config('admin.mgc.url') . $token;
        $bodys = array(
            'text' => $data['content']
        );
        $res = Common::curlPost($url, $bodys);
        //结果转成数组
        $res = json_decode($res, true);
        //根据自己的业务逻辑进行处理
       if($res['conclusion']=='不合规'){
           return ['code'=>4004,'msg'=>'出现敏感词'];
       }
        Problem::create(['user_id'=>$data['user_id'],'phone'=>$data['phone'],'conent'=>$data['content']]);
        return ['code'=>200,'msg'=>'ok','data'=>''];
    }

    //我的发布
    public function myAnnounce(Request $request)
    {
        $data = $request->except('token');
        if(!is_numeric($data['user_id']) || empty($data['user_id'])){
            return ['code'=>4005,'msg'=>'缺少参数'];
        }
        $res = MyAnnounce::where('user_id',$data['user_id'])->get()->toArray();
        return ['code'=>200,'msg'=>'ok','data'=>$res];
    }

    //收藏
    public function allLike(Request $request)
    {
        $data = $request->except('token');
        if(!is_numeric($data['user_id']) || empty($data['user_id'])){
            return ['code'=>4005,'msg'=>'缺少参数'];
        }

        $res = CollectHouse::with('like')
            ->where('user_no',$data['user_id'])
            ->get()
            ->toArray();
        $new=[];
        foreach ($res as $k=>$v){
            array_push($new,$v['like']);
        }
        return ['code'=>200,'msg'=>'ok','data'=>$new];
    }

    //h_id 房id
    public function addHistory(Request $request)
    {
        $data = $request->except('token');
        if(!is_numeric($data['user_id']) || empty($data['user_id'])){
            return ['code'=>4005,'msg'=>'缺少参数'];
        }
        $redis = new Client(['host'=>'127.0.0.1','port'=>6379]);
        $his = House::where('id',$data['h_id'])->first()->toArray();
        $redis->lpush('key'.$data['user_id'],json_encode($his));

    }

    public function history(Request $request)
    {
        $data = $request->except('token');
        if(!is_numeric($data['user_id']) || empty($data['user_id'])){
            return ['code'=>4005,'msg'=>'缺少参数'];
        }
        $redis = new Client(['host'=>'127.0.0.1','port'=>6379]);
        $res = $redis->lrange('key'.$data['user_id'],0,-1);
        $new=[];
        foreach ($res as $k=>$v){
            array_push($new,json_decode($v,true));
        }
        return ['code'=>200,'msg'=>'ok','data'=>$new];
    }

}
